<?php

/**
* alltosun.com 企业设置 set.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 钱有明 (qianym@alltosun.com) $
* $Date: 2014-8-1 下午4:45:08 $
* $Id$
*/

class Action
{
    protected $user_id;
    protected $company_id;

    /**
     * 登陆人员权限控制
     * @throws AnException
     */
    public function __construct()
    {
        $this->user_id = user_helper::get_user_id();
        $this->company_id = company_helper::get_company_id($this->user_id);

        if (empty($this->user_id) || !user_helper::is_company_manager($this->company_id, $this->user_id)) {
            //throw new AnMessageException('请以管理员身份登陆', 'error', AnUrl('user/login'));
        }
    }

    /**
     * 显示企业设置页
     * @param string $action
     * @param string $param
     */
    function __call($action = '', $param = array())
    {
        $company_info = _uri('company', $this->company_id);

        Response::assign('set_action', 'info');
        Response::assign('company_info', $company_info);

        Response::display('set.html');
    }

    /**
     * 保存企业信息
     */
    public function save_info()
    {
        $info = Request::Post('info', array());

        foreach ($info as $k=>$v) {
            if (!is_numeric($v)) {
                $info[$k] = AnFilter::filter_string($v);
            }
        }

        if (isset($info['email']) && $info['email']) {
            if (!filter_var($info['email'], FILTER_VALIDATE_EMAIL)) {
                return "邮箱输入格式不正确！";
            }
        }

        $res = _model('company')->update($this->company_id, $info);

        if (Request::isAjax()) {
            return 'ok';
        }

        Response::redirect(AnUrl('company/set'));
    }

    /**
     * 上下级关系
     */
    public function level()
    {
        $company_info = _uri('company', $this->company_id);

        Response::assign('set_action', 'level');
        Response::assign('company_info', $company_info);

        Response::display('set.html');
    }

    /**
     * 保存上下级关系
     */
    public function save_level()
    {
        $info = Request::Post('info', array());

        foreach ($info as $k=>$v) {
            if (!is_numeric($v)) {
                $info[$k] = AnFilter::filter_string($v);
            }
        }

        $res = _model('company')->update($this->company_id, $info);

        if (Request::isAjax()) {
            return 'ok';
        }

        Response::redirect(AnUrl('company/set'));
    }

    /**
     * 企业认证
     */
    public function auth()
    {
        $company_info = _uri('company', $this->company_id);

        Response::assign('set_action', 'auth');
        Response::assign('company_info', $company_info);

        Response::display('set.html');
    }

    /**
     * 保存企业信息
     */
    public function save_auth()
    {
        $info = Request::Post('info', array());

        foreach ($info as $k=>$v) {
            if (!is_numeric($v)) {
                $info[$k] = AnFilter::filter_string($v);
            }
        }

        $res = _model('company')->update($this->company_id, $info);

        if (Request::isAjax()) {
            return 'ok';
        }

        Response::redirect(AnUrl('company/set'));
    }
}
?>